# SPDX-License-Identifier: Apache-2.0
"""
create organization manual activation table

Revision ID: 6713c727bad2
Revises: 30c837b1425a
Create Date: 2025-08-27 21:06:14.999353
"""

import sqlalchemy as sa

from alembic import op

revision = "6713c727bad2"
down_revision = "30c837b1425a"

# Note: It is VERY important to ensure that a migration does not lock for a
#       long period of time and to ensure that each individual migration does
#       not break compatibility with the *previous* version of the code base.
#       This is because the migrations will be ran automatically as part of the
#       deployment process, but while the previous version of the code is still
#       up and running. Thus backwards incompatible changes must be broken up
#       over multiple migrations inside of multiple pull requests in order to
#       phase them in over multiple deploys.
#
#       By default, migrations cannot wait more than 4s on acquiring a lock
#       and each individual statement cannot take more than 5s. This helps
#       prevent situations where a slow migration takes the entire site down.
#
#       If you need to increase this timeout for a migration, you can do so
#       by adding:
#
#           op.execute("SET statement_timeout = 5000")
#           op.execute("SET lock_timeout = 4000")
#
#       To whatever values are reasonable for this migration as part of your
#       migration.


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "organization_manual_activations",
        sa.Column(
            "organization_id",
            sa.UUID(),
            nullable=False,
            comment="Foreign key to organization",
        ),
        sa.Column(
            "seat_limit",
            sa.Integer(),
            nullable=False,
            comment="Maximum number of organization members allowed",
        ),
        sa.Column(
            "expires",
            sa.Date(),
            nullable=False,
            comment="Expiration date for the manual activation",
        ),
        sa.Column(
            "created",
            sa.DateTime(),
            server_default=sa.text("now()"),
            nullable=False,
            comment="Datetime when manual activation was created",
        ),
        sa.Column(
            "created_by_id",
            sa.UUID(),
            nullable=False,
            comment="Admin user who created the manual activation",
        ),
        sa.Column(
            "id", sa.UUID(), server_default=sa.text("gen_random_uuid()"), nullable=False
        ),
        sa.ForeignKeyConstraint(
            ["created_by_id"],
            ["users.id"],
        ),
        sa.ForeignKeyConstraint(
            ["organization_id"], ["organizations.id"], ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("organization_id"),
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("organization_manual_activations")
    # ### end Alembic commands ###
